package com.grad.webstudy.controller;

import com.github.pagehelper.PageInfo;
import com.grad.webstudy.annota.ApiAuthorization;
import com.grad.webstudy.annota.LoginAuthorization;
import com.grad.webstudy.constants.AuthConstants;
import com.grad.webstudy.constants.FileContants;
import com.grad.webstudy.dto.CourseDto;
import com.grad.webstudy.enums.StatusEnums;
import com.grad.webstudy.exceptions.CustomException;
import com.grad.webstudy.model.Course;
import com.grad.webstudy.service.CourseService;
import com.grad.webstudy.utils.FileUtils;
import com.grad.webstudy.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.java2d.loops.CustomComponent;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.io.IOException;
import java.util.List;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/2/6 15:59.
 */

@RestController
@RequestMapping("/course")
@Validated
public class CourseController {


    @Autowired
    private CourseService courseService;


    @PostMapping("/add")
    @ApiAuthorization(auth = AuthConstants.COURSE_ADD)
    public int add(Course course, MultipartFile coverImage) {
        List<String> paths = FileUtils.upload(new MultipartFile[]{coverImage}, FileContants.COURSE_COVER_PATH);
        course.setCoverPath(paths.get(0));
        return courseService.addCourse(course);
    }


    @DeleteMapping("/delete")
    @ApiAuthorization(auth = AuthConstants.COURSE_DELETE)
    public boolean deleteIds(String ids) {
        return courseService.deleteCourses(ids);
    }


    @PostMapping("/update")
    @ApiAuthorization(auth = AuthConstants.COURSE_UPDATE)
    public boolean updateCourseInfo(CourseDto courseDto, MultipartFile coverImage) {

        if (!ObjectUtils.isEmpty(coverImage)){
            List<String> paths = FileUtils.upload(new MultipartFile[]{coverImage}, FileContants.COURSE_COVER_PATH);
            courseDto.setCoverPath(paths.get(0));
        }
        return courseService.updateCourseInfo(courseDto);
    }


    @PutMapping("/addStudyNumber")
    @LoginAuthorization
    public boolean updateStudyNumber(Integer courseId) {
        return courseService.updateStudyNumber(courseId);
    }


    @PutMapping("/updateScore")
    @LoginAuthorization
    public boolean updateScore(Integer courseId, Float score) {
        return courseService.updateScore(courseId, score);
    }


    /**
     * 对普通用户提供的查询接口
     *
     * @param currPage
     * @param pageSize
     * @return
     */
    @GetMapping("/queryAllByPageForUser")
    public PageInfo queryAllByPageForUser(@RequestParam(defaultValue = "1") int currPage, @RequestParam(defaultValue = "5") int pageSize
            , Integer level, Integer typeId) {
        return courseService.queryAllByPage(currPage, pageSize,
                StatusEnums.ONLINE.getStatus(), level, typeId,null);
    }


    /**
     * 对管理员提供的接口
     *
     * @param currPage
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/queryAllByPageForAdmin")
    @ApiAuthorization(auth = AuthConstants.COURSE_MANAGER)
    public PageInfo queryAllByPageForAdmin(@RequestParam(defaultValue = "1") int currPage,
                                           @RequestParam(defaultValue = "5") int pageSize,
                                           Integer status, Integer level, Integer typeId,String keyword) {
        return courseService.queryAllByPage(currPage, pageSize, status, level, typeId, keyword.trim());
    }



    /**
     * 对普通用户的接口
     *
     * @param currPage
     * @param pageSize
     * @param typeId
     * @return
     */
    @GetMapping("/queryAllByTypeForUser")
    public PageInfo queryAllByTypeForUser(@RequestParam(defaultValue = "1") int currPage,
                                          @RequestParam(defaultValue = "5") int pageSize,
                                          Integer typeId) {
        return courseService.queryCourseByType(currPage, pageSize, typeId, null);
    }


    /**
     * 对管理员提供的接口
     *
     * @param currPage
     * @param pageSize
     * @param typeId
     * @param status
     * @return
     */
    @GetMapping("/queryAllByTypeForAdmin")
    public PageInfo queryAllByTypeForAdmin(@RequestParam(defaultValue = "1") int currPage,
                                           @RequestParam(defaultValue = "5") int pageSize,
                                           Integer typeId, Integer status) {
        return courseService.queryCourseByType(currPage, pageSize, typeId, status);
    }


    /**
     * 根据id查询课程信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("/queryCourseById")
    @LoginAuthorization
    public CourseVo queryCourseById(Integer courseId) {
        return courseService.queryById(courseId,2);
    }

    @GetMapping("/queryCourseByIdForAdmin")
    @LoginAuthorization
    public CourseVo queryCourseByIdForAdmin(Integer courseId){
        return courseService.queryById(courseId,null);
    }


    @PutMapping("/changeCourseStatus")
    @ApiAuthorization(auth = AuthConstants.COURSE_MANAGER)
    public boolean changeCourseStatus(Integer courseId, @Min(2) @Max(3) Integer status) {
        return courseService.changeCourseStatus(courseId, status);
    }

    /**
     * 统计课程数量
     * @return
     */
    @GetMapping("/queryCourseCount")
    public Integer queryCourseCount(){
        return courseService.queryCourseCount();
    }


    @GetMapping("/queryCourseNew")
    public List<CourseVo> queryCourseNew(){
        return courseService.queryCourseNew();
    }


    @GetMapping("/queryCourselevel")
    public List<CourseVo> queryCourseLevel(String level){
        return courseService.queryCourseLevel(level);
    }


    @GetMapping("/queryCourseFront")
    public List<CourseVo> queryCourseFront(){
        return courseService.queryCourseFront();
    }
}
